//
//  ZHProductPKViewController.m
//  sc-cyan-ios
//
//  Created by lj on 2017/12/27.
//  Copyright © 2017年 greenbuilding. All rights reserved.
//

#import "ZHProductPKViewController.h"

#import "ZHSheetView.h"

#import "ZHProductPK.h"

@interface ZHProductPKViewController ()<ZHSheetViewDelegate,ZHSheetViewDataSource>

//左侧标题
@property (nonatomic,strong) NSArray *leftTitleList;
//上部标题
@property (nonatomic,strong) NSMutableArray *topTitleList;
//表格视图
@property (nonatomic,weak) ZHSheetView *sheetView;

@end

@implementation ZHProductPKViewController

- (NSArray *)leftTitleList{
    if (!_leftTitleList) {
        _leftTitleList = @[@"图片",@"序列号",@"出租方式",@"租赁价格",@"出厂时间",@"地区",@"配备操作人员",@"与您的距离"];
    }
    return _leftTitleList;
}


- (NSArray *)topTitleList{
    if (!_topTitleList) {
        
        _topTitleList = [NSMutableArray array];
        for (int i = 0; i < 10; i++) {
            NSDictionary *dic = @{@"title":@"傻逼了吧",@"image":@"swcs1.jpg",@"index":@"1",@"way":@"月",@"price":@"1000",@"time":@"2017",@"isPerson":@"1",@"distance":@"100km",@"area":@"贵州贵阳南明区"};
            
            [_topTitleList addObject:dic];
        }
        
        _topTitleList = [ZHProductPK mj_objectArrayWithKeyValuesArray:_topTitleList];
    }
    return _topTitleList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpNav];
    
    [self setUpView];
}

- (void)setUpNav{
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTitle:@"清空" target:self action: @selector(clearAction)];
}

- (void)setUpView{
    self.view.backgroundColor = [UIColor whiteColor];
    
    ZHSheetView *sheetView = [[ZHSheetView alloc] initWithFrame:self.view.bounds];
    sheetView.dataSource = self;
    sheetView.delegate = self;
    sheetView.sheetHead = @"产品信息";
    sheetView.titleRowHeight = 65;
    sheetView.titleColWidth = 77;
    sheetView.lineColor = UIColorFromRGB(0xeeeeee);
    [self.view addSubview:sheetView];
    self.sheetView = sheetView;
}
//返回行
- (NSInteger)sheetView:(ZHSheetView *)sheetView numberOfRowsInSection:(NSInteger)section
{
    return self.leftTitleList.count;
}
//返回列
- (NSInteger)sheetView:(ZHSheetView *)sheetView numberOfColsInSection:(NSInteger)section
{
    return self.topTitleList.count;
}
//返回每个格子的视图
- (UIView *)sheetView:(ZHSheetView *)sheetView cellForContentItemAtIndexRow:(NSIndexPath *)indexRow indexCol:(NSIndexPath *)indexCol rect:(CGRect)rect{
    
    CGFloat x = 4;
    CGFloat y = x;
    CGFloat w = rect.size.width - x * 2;
    CGFloat h = rect.size.height - y * 2;
    CGRect rec = CGRectMake(x, y, w, h);
    
    if (indexRow.row == 0) {
        UIImageView *iconView = [[UIImageView alloc] initWithFrame:rec];
        iconView.image = [UIImage imageNamed:@"swcs1.jpg"];
        return  iconView;
    }
    else{
        UILabel *lable = [[UILabel alloc] initWithFrame:rec];
        lable.font = SYSTEMFONT(12);
        lable.textAlignment = NSTextAlignmentCenter;
        lable.text = [NSString stringWithFormat:@"data(%ld,%ld)", (long)indexRow.row, (long)indexCol.row];
        if (indexRow.row == 3) {
            lable.textColor = UIColorFromRGB(0xff6559);
        }else{
            lable.textColor = [UIColor gb_textColor_999999];
        }
        return lable;
    }
}

- (NSString *)sheetView:(ZHSheetView *)sheetView cellForLeftColAtIndexPath:(NSIndexPath*)indexPath
{
    return self.leftTitleList[indexPath.row];
}

- (NSString *)sheetView:(ZHSheetView *)sheetView cellForTopRowAtIndexPath:(NSIndexPath*)indexPath
{
    ZHProductPK *molde = self.topTitleList[indexPath.row];
    return molde.title;
}

//返回每行的高
- (CGFloat)sheetView:(ZHSheetView *)sheetView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            return 71;
            break;
        case 1:
        case 2:
        case 3:
        case 4:
            return 45;
            break;
        case 5:
            //这里需要计算文字的高度是否大于45
            return 45;
            break;
        default:
            return 60;
            break;
    }
}

//返回没列的宽
- (CGFloat)sheetView:(ZHSheetView *)sheetView widthForColAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (BOOL)sheetView:(ZHSheetView *)sheetView cellWithColorAtIndexRow:(NSIndexPath *)indexRow
{
    return (indexRow.row%2 != 0)?YES:NO;
}

-(void)sheetView:(ZHSheetView *)sheetView didSelectItemAtIndexRow:(NSIndexPath *)indexRow indexCol:(NSIndexPath *)indexCol
{
    NSLog(@"点击 row %ld, col %ld", (long)indexRow.row, (long)indexCol.row);
}

//删除列
- (void)sheetView:(ZHSheetView *)sheetView cellWithDidDeleteAtIndexRow:(NSIndexPath *)indexPath {
    
    [self.topTitleList removeObjectAtIndex:indexPath.item];
    
    [self.sheetView reloadData];

}

#pragma mark =============清除按钮触发事件===============
- (void)clearAction{
    
//    DLog(@" %s",__func__);
    [self.topTitleList removeAllObjects];
    [self.sheetView reloadData];
}

@end
